package cn.work.dao;

import cn.work.bean.userbean;
import cn.work.util.DataBaseutil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*用户增删改查数据库方法*/
public class userDao {

//    查询用户账号密码是否正确
    public static userbean selectlogin(userbean user) {
        System.out.printf("dao层账号为：%s，密码为%s\n",user.getUsername(),user.getPassword());//测试信息
        String sql = "SELECT * FROM user WHERE username = '" + user.getUsername() + "' AND password = '" + user.getPassword() + "'";
        userbean resultUser = null;
        Connection connection = DataBaseutil.getConnection();
        if (connection!= null) {
            System.out.println("数据库连接成功");
            try {
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(sql);
                if (resultSet.next()) {
                    // 如果查询到结果，创建一个userbean对象并设置属性
                    resultUser = new userbean();
                    resultUser.setPassword(resultSet.getString("password"));
                    resultUser.setUsername(resultSet.getString("username"));
                    resultUser.setIdentity(resultSet.getInt("identity"));
                    resultUser.setUid(resultSet.getInt("uid"));
                }
                else{
                    System.out.print("sql查询无结果");
                }
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return resultUser;
    }
}
